/*
 * This file is part of aion-unique <aion-unique.org>.
 *
 *  aion-unique is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  aion-unique is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with aion-unique.  If not, see <http://www.gnu.org/licenses/>.
 */
package system.handlers.admincommands;

import java.util.Iterator;
import java.util.List;

import com.aionemu.gameserver.configs.administration.AdminConfig;
import com.aionemu.gameserver.model.gameobjects.Item;
import com.aionemu.gameserver.model.gameobjects.player.Player;
import com.aionemu.gameserver.model.gameobjects.player.SkillListEntry;
import com.aionemu.gameserver.model.team.group.PlayerGroup;
import com.aionemu.gameserver.model.team.legion.Legion;
import com.aionemu.gameserver.model.team.legion.LegionMember;
import com.aionemu.gameserver.utils.PacketSendUtility;
import com.aionemu.gameserver.utils.Util;
import com.aionemu.gameserver.utils.chathandlers.AdminCommand;
import com.aionemu.gameserver.utils.chathandlers.AdminCommandChatHandler;
import com.aionemu.gameserver.world.World;

/**
 * @author lyahim
 * 
 */
public class PlayerInfo extends AdminCommand
{

	private final static int	showLineNumber	= 20;

	public PlayerInfo()
	{
		super("playerinfo");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.aionemu.gameserver.utils.chathandlers.AdminCommand#executeCommand
	 * (com.aionemu.gameserver.model.gameobjects.player.Player,
	 * java.lang.String[])
	 */
	@SuppressWarnings("rawtypes")
	@Override
	public void executeCommand(Player admin, String[] params)
	{
		if (admin.getAccessLevel() < AdminConfig.COMMAND_PLAYERINFO)
		{
			PacketSendUtility.sendMessage(admin, "You dont have enough rights to execute this command");
			return;
		}

		if (params == null || params.length < 1)
		{
			PacketSendUtility.sendMessage(admin, "syntax //playerinfo <playername> <loc | item | group | skill | legion> ");
			return;
		}

		Player target = World.getInstance().findPlayer(Util.convertName(params[0]));

		if (target == null)
		{
			PacketSendUtility.sendMessage(admin, "Selected player cannot online!");
			return;
		}

		PacketSendUtility.sendMessage(admin, "\n[Info about " + target.getName() + "]\n-common: lv" + target.getLevel() + "("
						+ target.getObjectTemplate().getExpShown() + " xp), " + target.getObjectTemplate().getRace() + ", "
				+ target.getObjectTemplate().getPlayerClass() + "\n-ip: " + target.getClientConnection().getHostAddress() + "\n" + "-account name: "
				+ target.getClientConnection().getAccount().getName() + "\n" + "-online: ~"
				+ (System.currentTimeMillis() - target.getObjectTemplate().getLastOnline()) / 60000 + " minute(s)\n");

		if (params.length < 2)
			return;

		if (params[1].equals("item"))
		{
			StringBuilder strbld = new StringBuilder("-items in inventory:\n");

			List<Item> items = target.getInventory().getAllItems();
			Iterator it = items.iterator();

			if (items.isEmpty())
				strbld.append("none\n");
			else
			{
				while (it.hasNext())
				{

					Item act = (Item) it.next();
					strbld.append("    " + act.getItemCount() + "(s) of " + "[item:" + act.getItemTemplate().getTemplateId() + "]" + "\n");
				}
			}
			items.clear();
			items = target.getEquipment().getEquippedItems();
			it = items.iterator();
			strbld.append("-equipped items:\n");
			if (items.isEmpty())
				strbld.append("none\n");
			else
			{
				while (it.hasNext())
				{
					Item act = (Item) it.next();
					strbld.append("    " + act.getItemCount() + "(s) of " + "[item:" + act.getItemTemplate().getTemplateId() + "]" + "\n");
				}
			}

			items.clear();
			items = target.getWarehouse().getAllItems();
			it = items.iterator();
			strbld.append("-items in warehouse:\n");
			if (items.isEmpty())
				strbld.append("none\n");
			else
			{
				while (it.hasNext())
				{
					Item act = (Item) it.next();
					strbld.append("    " + act.getItemCount() + "(s) of " + "[item:" + act.getItemTemplate().getTemplateId() + "]" + "\n");
				}
			}
			showAllLines(admin, strbld.toString());
		}
		else if (params[1].equals("group"))
		{
			StringBuilder strbld = new StringBuilder("-group info:\n  Leader: ");

			PlayerGroup group = target.getPlayerGroup();
			if (group == null)
				PacketSendUtility.sendMessage(admin, "-group info: no group");
			else
			{
				strbld.append(group.getLeader().getName() + "\n  Online members:\n");
				for (Player memb : group.getOnlineMembers())
					strbld.append("    " + memb.getName() + "\n");
				PacketSendUtility.sendMessage(admin, strbld.toString());
			}
		}
		else if (params[1].equals("skill"))
		{
			StringBuilder strbld = new StringBuilder("-list of skills:\n");

			SkillListEntry sle[] = target.getSkillList().getAllSkills();

			for (int i = 0; i < sle.length; i++)
			{
				strbld.append("    level " + sle[i].getSkillLevel() + " of " + sle[i].getSkillName() + "\n");
			}
			showAllLines(admin, strbld.toString());
		}
		else if (params[1].equals("loc"))
		{
			PacketSendUtility.sendMessage(admin, "-location:\n  mapid: " + target.getWorldId() + "\n  X: " + target.getX() + " Y: " + target.getY() + "Z: "
					+ target.getZ() + "heading: " + target.getHeading());
		}
		else if (params[1].equals("legion"))
		{
			StringBuilder strbld = new StringBuilder();

			Legion legion = target.getLegion();
			if (legion == null)
				PacketSendUtility.sendMessage(admin, "-legion info: no legion");
			else
			{
				strbld.append("-legion info:\n  name: " + legion.getName() + ", level: " + legion.getLegionLevel() + "\n  members(online):\n");
				for (LegionMember lmemb : legion.getMembers())
				{
					strbld.append("    " + lmemb.getCommondata().getName() + "(" + ((lmemb.isOnline() == true) ? "online" : "offline") + ")"
							+ lmemb.getRank().toString() + "\n");
				}
			}
			showAllLines(admin, strbld.toString());
		}
		else
		{
			PacketSendUtility.sendMessage(admin, "bad switch!");
			PacketSendUtility.sendMessage(admin, "syntax //playerinfo <playername> <loc | item | group | skill | legion> ");
		}
	}

	private void showAllLines(Player admin, String str)
	{
		int index = 0;
		String[] strarray = str.split("\n");

		while (index < strarray.length - showLineNumber)
		{
			StringBuilder strbld = new StringBuilder();
			for (int i = 0; i < showLineNumber; i++, index++)
			{
				strbld.append(strarray[index]);
				if (i < showLineNumber - 1)
					strbld.append("\n");
			}
			PacketSendUtility.sendMessage(admin, strbld.toString());
		}
		int odd = strarray.length - index;
		StringBuilder strbld = new StringBuilder();
		for (int i = 0; i < odd; i++, index++)
		{
			strbld.append(strarray[index] + "\n");
		}
		PacketSendUtility.sendMessage(admin, strbld.toString());
	}

	public static void main(String[] args)
	{
		AdminCommandChatHandler.getInstance().registerAdminCommand(new PlayerInfo());
	}
}
